package com.lz.autotest.common.utils;

import com.lz.autotest.project.testmanage.autoTest.entity.InfResultVo;
import com.lz.autotest.project.testmanage.autoTest.entity.ResultView;
import com.lz.autotest.project.testmanage.autoTest.service.Engine.XtsEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: X
 * @date: Created in 2023/7/12 9:14
 * @Description:
 */
public class GlobalMap {
    private static Map<String, AtomicInteger> globalMap = new ConcurrentHashMap<>();

    private static Map<String, String> envMap = new ConcurrentHashMap<>();

    private static Map<String, XtsEngine> engineMap = new ConcurrentHashMap<>();

    private static Map<String, InfResultVo> infResultMap = new ConcurrentHashMap<>();

    public static void put(String key, AtomicInteger value) {
        globalMap.put(key, value);
    }

    public static void putEnv(String key, String value) {
        envMap.put(key, value);
    }

    public static void putEngine(String key, XtsEngine value) {
        engineMap.put(key, value);
    }

    //完成修改状态
    public static synchronized void updateInfResult(String key, Integer isReoord, Integer isDone, Long time) {
        InfResultVo result = infResultMap.get(key);
        if (isReoord != null) {
            result.setIsRecord(isReoord);
        }
        if (isDone != null) {
            result.setIsDone(isDone);
        }
        if (time != null) {
            result.setTotalTime(time);
        }
    }
    //加锁处理
    public static synchronized void putInfResult(String key, ResultView value) {
        InfResultVo result = infResultMap.get(key);
        //当不记录信息时直接返回不存入
        if (result != null && result.getIsRecord() == 0) {
            return;
        }
        if (result == null) {
            InfResultVo resultVo = new InfResultVo();
            List<ResultView> list = new ArrayList<>();
            list.add(value);
            resultVo.setIsRecord(1);
            resultVo.setList(list);
            infResultMap.put(key, result);
        } else {
            result.getList().add(value);
        }
    }

    public static void putInfResultVo(String key, InfResultVo value) {
        infResultMap.put(key, value);
    }

    public static AtomicInteger get(String key) {
        return globalMap.get(key);
    }

    public static String getenv(String key) {
        return envMap.get(key);
    }

    public static XtsEngine getengine(String key) {
        return engineMap.get(key);
    }

    public static synchronized InfResultVo getInfResult(String key) {
        return infResultMap.get(key);
    }

    public static void remove(String key) {
        globalMap.remove(key);
    }

    public static void removeEnv(String key) {
        envMap.remove(key);
    }

    public static void removeEngine(String key) {
        engineMap.remove(key);
    }

    public static void removeInfResult(String key) {
        infResultMap.remove(key);
    }

    public static Set<String> keySet() {
        return globalMap.keySet();
    }

    public static Set<String> keySetEnv() {
        return envMap.keySet();
    }

    public static Set<String> keySetEngine() {
        return engineMap.keySet();
    }

    public static Set<String> keySetInfResult()  {
        return infResultMap.keySet();
    }
}
